package org.redmaple.core.retrieving.hibernate;

import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.redmaple.core.retrieving.RedmapleCriterion;

/**
 * @author Xiangyun
 *
 */
public class CriterionPlugin {

	@SuppressWarnings("unchecked")
	public static Criterion convert(RedmapleCriterion redmapleCriterion, String entityName) {
		Criterion ret = null;
		String propertyName = redmapleCriterion.getPropertyName();
		propertyName = entityName + "." + propertyName;
		Object value = redmapleCriterion.getValue();
		List<Object> listVal = null;
		switch(redmapleCriterion.getRetrievingType()){
		case BETWEEN:
			listVal = (List<Object>) value;
			ret = Restrictions.between(propertyName, listVal.get(0), listVal.get(1));
			break;
		case EQ:
			ret = Restrictions.eq(propertyName, value);
			break;
		case GE:
			ret = Restrictions.ge(propertyName, value);
			break;
		case GT:
			ret = Restrictions.gt(propertyName, value);
			break;
		case IN:
			listVal = (List<Object>) value;
			ret = Restrictions.in(propertyName, listVal);
			break;
		case ISNOTNULL:
			ret = Restrictions.isNotNull(propertyName);
			break;
		case ISNULL:
			ret = Restrictions.isNull(propertyName);
			break;
		case LE:
			ret = Restrictions.le(propertyName, value);
			break;
		case LIKE:
		case LLIKE:
		case RLIKE:
			ret = Restrictions.like(propertyName, value);
			break;
		case LT:
			ret = Restrictions.lt(propertyName, value);
			break;
		case NEQ:
			ret = Restrictions.ne(propertyName, value);
			break;
		default:
			break;
		}
		return ret;
	}
}
